package com.ziyi.tiantianshuiyin.base;


import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;


import com.android.tu.loadingdialog.LoadingDailog;
import com.gtdev5.geetolsdk.mylibrary.util.ToastUtils;
import com.gtdev5.geetolsdk.mylibrary.widget.CenterDialog;
import com.gtdev5.geetolsdk.mylibrary.widget.OnDialogClickListener;
import com.ziyi.tiantianshuiyin.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment_1 extends Fragment implements View.OnClickListener {
    public Activity mActivity;
    public Context mContext;
    public Bundle savedInstanceState;
    public LoadingDailog dailog;
    public View mContentView;
    public Unbinder unbinder;

    /**
     * 视图是否已经初初始化
     */
    protected boolean isInit = false;
    protected boolean isLoad = false;
    protected final String TAG = "BaseFragment";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContentView = inflater.inflate(setLayoutResourceID(), container, false);
        unbinder = ButterKnife.bind(this, mContentView);
        mActivity = getActivity();
        mContext = getContext();
        this.savedInstanceState = savedInstanceState;
        getLoadingDailog();
        initView();
        isInit = true;
        /**初始化的时候去加载数据**/
        isCanLoadData();
        return mContentView;
    }

    /**
     * 视图是否已经对用户可见，系统的方法
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isCanLoadData();
    }

    /**
     * 是否可以加载数据
     * 可以加载数据的条件：
     * 1.视图已经初始化
     * 2.视图对用户可见
     */
    private void isCanLoadData() {
        if (!isInit) {
            return;
        }
        if (getUserVisibleHint()) {
            if (!isLoadData()) {
                lazyLoad();
            }
            isLoad = true;
        } else {
            if (isLoad) {
                stopLoad();
            }
        }
    }

    public static String getMobileType() {
        return Build.MANUFACTURER;
    }

    /**
     * 是否要每次进来加载lazyData
     * @return
     */
    protected abstract boolean isLoadData();


    protected abstract int setLayoutResourceID();

    public void getLoadingDailog() {
        LoadingDailog.Builder loadBuilder = new LoadingDailog.Builder(mActivity)
                .setMessage("加载中...")
                .setCancelable(true)
                .setCancelOutside(true);
        dailog = loadBuilder.create();
    }


    protected final void logShow(String msg) {
        Log.e("weather", msg);
    }

    @Override
    public void onClick(View v) {

    }
    /**
     * sessionId 过期
     */
    protected final void LoginOut() {
        ToastUtils.showShortToast("身份已过期,请重新登录");

//        PreferencesUtil.getInstance(mActivity).clear();
//        Intent intent = new Intent(mActivity,LoginActivity.class);
//        intent.putExtra("loginout_type",1);
//        startActivity(intent);
//        JMessageClient.logout(); 极光
//        AppManagerUtil.instance().finishAllActivity();
    }

    protected abstract void initView();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        isInit = false;
        isLoad = false;

    }

    /**
     * 当视图初始化并且对用户可见的时候去真正的加载数据
     */
    protected abstract void lazyLoad();

    /**
     * 当视图已经对用户不可见并且加载过数据，如果需要在切换到其他页面时停止加载数据，可以调用此方法
     */
    protected void stopLoad() {
    }

    protected final String getTextByEditText(EditText et) {
        return et.getText().toString().trim();
    }

    /**
     * 提示弹框
     */
    public void ShowTipDialog(String name, String content, String btnText, OnDialogClickListener listener) {
        int[] ids = new int[]{
                R.id.dialog_bt_dis,
                R.id.dialog_bt_ok
        };
        CenterDialog dialog = new CenterDialog(getActivity(), R.layout.dialog_show_tips, ids, false);
        dialog.setOnCenterClickListener((dial, view) -> {
            if (view.getId() == R.id.dialog_bt_ok) {
                listener.OnDialogOK();
                if (!getActivity().isFinishing()) {
                    dialog.dismiss();
                }
            }
            if (view.getId() == R.id.dialog_bt_dis) {
                if (!getActivity().isFinishing()) {
                    dialog.dismiss();
                }
                listener.OnDialogExit();
            }
        });
        if (!getActivity().isFinishing()) {
            dialog.show();
            dialog.setText(R.id.dialog_tv_title, name);
            dialog.setText(R.id.dialog_tv_text, content);
            dialog.setText(R.id.dialog_bt_ok, btnText);
        }
    }

}
